package com.example.otto.controller;

import com.example.otto.Jwt.JwtUtil;
import com.example.otto.entity.Manager;
import com.example.otto.entity.Worker;
import com.example.otto.mapper.WorkerMapper;
import com.example.otto.service.WorkerService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/worker")
public class WorkerController {

    @Autowired
    private WorkerMapper workerMapper;

    @Autowired
    private WorkerService workerService;

    JwtUtil jwtUtil = new JwtUtil();

    //查看所有维修人员
    @GetMapping
    public ResponseEntity<List<Worker>> index(@RequestHeader(name = "Authorization", required = false) String authHeader) {
        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(workerMapper.findAll(), HttpStatus.OK);
    }

    //查询忙碌的维修人员
    @GetMapping("/work")
    public ResponseEntity<List<Worker>> get_busy_worker(@RequestHeader(name = "Authorization", required = false) String authHeader){

        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(workerMapper.findBusy(), HttpStatus.OK);
    }

    //查询空闲的维修人员
    @GetMapping("/free")
    public  ResponseEntity<List<Worker>> get_free_worker(@RequestHeader(name = "Authorization", required = false) String authHeader){
        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(workerMapper.findFree(), HttpStatus.OK);
    }

    //维修人员忙碌
    @PutMapping("/work")
    public ResponseEntity<Boolean> set_worker_busy(@RequestParam String id,
                                                   @RequestHeader(name = "Authorization", required = false) String authHeader){
        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(workerMapper.setBusy(id), HttpStatus.OK);
    }

    //维修人员空闲
    @PutMapping("/free")
    public  ResponseEntity<Boolean> set_worker_free(@RequestParam String id,
                                   @RequestHeader(name = "Authorization", required = false) String authHeader) {
        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(workerMapper.setFree(id), HttpStatus.OK);
    }

    //获取维修人员状态
    @GetMapping("/get_worker_state")
    public ResponseEntity<Integer> getWorkerState(@RequestParam Integer id,
                                                  @RequestHeader(name = "Authorization", required = false) String authHeader){

        Claims claims = jwtUtil.Verify(authHeader);

        // 如果Token验证失败，返回401 Unauthorized
        if (claims == null) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }

        return new ResponseEntity<>(workerMapper.getState(id),HttpStatus.OK);
    }

    //维修人请假
//    @PutMapping("/rest")
//    public ResponseEntity<Boolean> askForRest(@RequestParam Integer id,
//                                              @RequestHeader(name = "Authorization", required = false) String authHeader){
//        Claims claims = jwtUtil.Verify(authHeader);
//
//        // 如果Token验证失败，返回401 Unauthorized
//        if (claims == null) {
//            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
//        }
//
//        return new ResponseEntity<>(workerMapper.,HttpStatus.OK);
//    }

    //维修人员登录
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody Worker worker){
        return workerService.loginService(worker);
    }


}
